package message;

import model.RoomInfo;
import socket.ChessServer;

/**
 * 游戏结束报文,由胜者发送.
 *
 * @author 张紘溢
 */
public class GameOverMessage extends BaseMessage {
    private int roomId;
    private boolean isLeft;

    public GameOverMessage(int roomId, boolean isLeft) {
        this.roomId = roomId;
        this.isLeft = isLeft;
    }

    @Override
    public void sendToServer() {
        RoomInfo roomInfo = ChessServer.getInstance().getRooms().get(roomId);
        roomInfo.setLeftReady(false);
        roomInfo.setRightReady(false);
        ChessServer.getInstance().getRooms().set(roomId, roomInfo);
        if (isLeft) {
            //向自己发送获胜报文
            ServerWinMessage serverWinMessage = new ServerWinMessage();
            ChessServer.getInstance().sendMsgToClient(serverWinMessage, roomInfo.getLeftPlayer());
            //向对方发送失败报文
            ServerDefeatMessage serverDefeatMessage = new ServerDefeatMessage();
            ChessServer.getInstance().sendMsgToClient(serverDefeatMessage, roomInfo.getRightPlayer());
        } else {
            ServerWinMessage serverWinMessage = new ServerWinMessage();
            ChessServer.getInstance().sendMsgToClient(serverWinMessage, roomInfo.getRightPlayer());
            ServerDefeatMessage serverDefeatMessage = new ServerDefeatMessage();
            ChessServer.getInstance().sendMsgToClient(serverDefeatMessage, roomInfo.getLeftPlayer());
        }
    }

    @Override
    public void selfExecute() {

    }
}
